﻿using System;
using System.Web.UI.WebControls;
using Pharmacy.Web.UI;
using Pharmacy.Entities;
using Pharmacy.Data;

public partial class Admin_ReturnGood : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FormUtil.RedirectAfterUpdate(GridView1, "ReturnGood.aspx?page={0}");
        FormUtil.SetPageIndex(GridView1, "page");
        FormUtil.SetDefaultButton((Button)GridViewSearchPanel1.FindControl("cmdSearch"));
    }
    protected string GetWhereClause()
    {
        string WhereClause = "left(StockOutId,1)='R'"; //lay gia tri dau tien cua prefix
        return WhereClause;
    }
    protected void StockOutDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (String.IsNullOrEmpty(e.InputParameters["WhereClause"].ToString()))
            e.InputParameters["WhereClause"] = GetWhereClause();
        else
            e.InputParameters["WhereClause"] += " And " + GetWhereClause();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string urlParams = string.Format("StockOutId={0}", GridView1.SelectedDataKey.Values[0]);
        Response.Redirect("ReturnGoodEdit.aspx?" + urlParams, true);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string stockOutID = e.CommandArgument.ToString().Trim();
        if (e.CommandName == "CustomDelete")
        {
            TList<Pharmacy.Entities.StockOutDetails> objStockOutdetail = DataRepository.StockOutDetailsProvider.GetByStockOutId(stockOutID);
            if (objStockOutdetail.Count > 0)
            {
                Response.Write(@"<script language='javascript'>alert('Vui lòng xóa tất cả chi tiết.')</script>");

            }
            else
            {
                Pharmacy.Entities.StockOut objStockOut = new Pharmacy.Entities.StockOut();
                objStockOut.StockOutId = stockOutID;
                DataRepository.StockOutProvider.Delete(objStockOut);
            }
            GridView1.DataBind();
        }
    }
}
